﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Windows;
using WebSocket4Net;

namespace Chat
{
    class Method
    {
        string host;

        bool running = true;
        public static void LogoutExit(WebSocket webSocket)
        {
            if (webSocket.State == WebSocketState.Open)
            {
                webSocket.Send("Logout\n\n");
                webSocket.Close();
            }
            App.Current.Shutdown();
        }
        public static void ErrorProcessor(object sender, MessageReceivedEventArgs e)
        {
            string[] er = e.Message.Split('\n');
            if (er[0] == "Error")
            {
                ICU.Errors err = (ICU.Errors)Convert.ToInt32(er[1]);
                switch (err)
                {
                    case ICU.Errors.Login_NoSuchUser:
                        MessageBox.Show("没有这个用户！");
                        break;

                    case ICU.Errors.Login_WrongPassword:
                        MessageBox.Show("密码错误，请再次确认！");
                        break;

                    case ICU.Errors.Login_Banned:
                        MessageBox.Show("您已被封禁！请联系管理员");
                        break;
                    case ICU.Errors.Logout_AnotherLogin:
                    case ICU.Errors.Logout_Banned:
                    case ICU.Errors.Logout_ServerClosed:
                        MessageBox.Show($"因为{err.GetType().GetEnumName(err)}，您已被踢出服务器，客户端将自动关闭");
                        Application.Current.Shutdown(0);
                        break;
                    default:
                        MessageBox.Show(err.GetType().GetEnumName(err) ?? "Error: Unknown name.");
                        break;
                }
            }
        }
        public static void AutoReconnect(WebSocket webSocket, string thost, ConnectionStateControl control)
        {
            if (webSocket.State != WebSocketState.Open)
            {
                bool compleed = webSocket.OpenAsync().Wait(10000);
                if (!compleed)
                {
                    MessageBox.Show("服务器无法连接，请检查网络...");
                }
            }
        }
    }
}
